Completed
Push — master ( 782061...5c46be )
by
unknown
13:53
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
$(document).ready(function() {
2
    $(document).on('click', 'a[data-gototab]', function() {
3
        var tabName = $(this).attr('data-gototab');
4
        $('li[data-tab-name="' + tabName + '"] a').trigger('click');
5
    });
6
7
    var countCollectionsInTabs = function() {
8
        var countableTabs = $('.countable-tab');
9
10
        countableTabs.each(function(index) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
11
            var tab = $(this);
12
            tab.data('currentCount', 0);
13
14
            var countableClasses = $.grep(tab.attr("class").split(' '), function(v) {
15
                return v.match(/count-.*/);
16
            });
17
18
            $.each(countableClasses, function(i, cls) {
19
                var collectionAsForm, collectionAsTable = 0;
20
                var fieldName = cls.replace('count-', '');
21
22
                if (Admin.currentAdmin) {
0 ignored issues
show
Bug introduced by
The variable Admin seems to be never declared. If this is a global, consider adding a /** global: Admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
23
                    var fieldId = 'field_widget_' + Admin.currentAdmin.uniqid + '_' + fieldName;
24
25
                    collectionAsForm = $('#' + fieldId + ' > div.sonata-ba-tabs > div');
26
                    collectionAsTable = $('#' + fieldId + ' > table > tbody > tr');
27
                } else {
28
                    var field = $('*[data-field-name="' + fieldName + '"]');
29
30
                    collectionAsForm = field.find('> div.box');
31
                    collectionAsTable = '';
32
                }
33
34
                tab.data('currentCount', parseInt(tab.data('currentCount'), 10) + (
35
                    parseInt(collectionAsForm.length, 10) +
36
                    parseInt(collectionAsTable.length, 10)
37
                ));
38
            });
39
40
            var counterItem = tab.find('a > span.counter');
41
42
            if (counterItem.length == 0) {
43
                counterItem = tab.find('a').append(
44
                    $('<span/>').attr({
45
                        'class': 'counter'
46
                    })
47
                ).find('.counter');
48
            }
49
50
            counterItem.html(tab.data('currentCount')).attr('data-count', tab.data('currentCount'));
51
        });
52
    };
53
54
    countCollectionsInTabs();
55
56
    $(document).on('sonata.add_element', function() {
57
        countCollectionsInTabs();
58
    });
59
60
    var cookieLastTab = getCookie('selectedTab');
61
62
    if (cookieLastTab) {
63
        $('.nav-tabs li[data-tab-name="'+cookieLastTab+'"] a').trigger('click');
64
    }
65
66
    $(document).on('click', '.nav-tabs li[data-tab-name] a', function() {
67
        document.cookie = 'selectedTab=' + $(this).closest('li').data('tabName');
68
    });
69
});
70
71
function getCookie(cname) {
72
    var name = cname + "=";
73
    var decodedCookie = decodeURIComponent(document.cookie);
74
    var ca = decodedCookie.split(';');
75
    for (var i = 0; i < ca.length; i++) {
76
        var c = ca[i];
77
        while (c.charAt(0) == ' ') {
78
            c = c.substring(1);
79
        }
80
        if (c.indexOf(name) == 0) {
81
            return c.substring(name.length, c.length);
82
        }
83
    }
84
    return null;
85
}
86